package com.xy.mvvmdemo.bean;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.util.HashMap;

/**
 * @ClassName LiveDataBusBeta
 * @Author xy
 * @Date 2021/7/9 10:24
 * @Description TODO
 */
public class LiveDataBusBeta {

    private final HashMap<String, LiveData<Object>> bus;

    private LiveDataBusBeta() {
        bus = new HashMap<String, LiveData<Object>>();
    }

    private static class SingleHolder {
        private static final LiveDataBusBeta DATA_BUS = new LiveDataBusBeta();
    }

    public static LiveDataBusBeta getInstance() {
        return SingleHolder.DATA_BUS;
    }


    public <T> MutableLiveData<T> with(String key, Class<T> type) {
        if (!bus.containsKey(key)) {
            bus.put(key, new MutableLiveData<Object>());
        }
        return (MutableLiveData<T>) bus.get(key);
    }

    public MutableLiveData<Object> with (String target){
        return with (target,Object.class);
    }

    public void remove(String key){
        if (bus.containsKey(key)){
            bus.remove(key);
        }
    }
}